<?php
namespace Apps\BoxSns\Model;

use Think\Model;

class SnsCommentModel extends \BaseModel
{
    public function load($id = "")
    {
        $data = $this->find($id);
        if (!$data) {
            return false;
        }
        $data['content'] = relpace_emotion($data['content']);
        $data['time_tran'] = time_tran($data['dateline']);
        /****发评论者***/
        if ($data['user_id']) {
            $data['user'] = BoxModel("SystemMember")->load($data['user_id']);
        }
        /*******被评用户***/
        if ($data['to_user_id']) {
            $data['to_user'] = BoxModel("SystemMember")->load($data['user_id']);
            $data['rname'] = $data['to_userInfo']['nickname'];
        }
        /*******被回复的内容***/
        if ($to_comment_id = $data['to_comment_id']) {
            $data['comment'] = $this->load($to_comment_id);
        }
        hook("comment_load", $data);
        hook("comment_load_" . $data['type'], $data);
        return $data;
    }


    /******设置为best***/
    public function best($type, $id)
    {
        $model = BoxModel("sns_comment");
        $r = $model->where("id = '$id'")->save(array('is_best' => 1));
        if ($r) {
            hook("comment_set_best", $id);
            hook("comment_set_best_" . $type, $id);
        }
        return $r;
    }

    /*******增加评论***/
    public function create_comment($row)
    {
        $row['postip'] = getIp();
        $pid = $row['pid'];
        $row['user_id'] = intval($row['user_id']);
        $row['to_user_id'] = intval($row['to_user_id']);
        $row['to_comment_id'] = intval($row['to_comment_id']);
        $row['dateline'] = time();
        $type = $row['type'];
        hook("comment_before_save", $row);
        hook("comment_before_save_" . $type, $row);
        $comment_type = dict("comment_type");
        //$this->make_table($type);
        $id = BoxModel("sns_comment")->add($row);
        if ($id) {
									   $row['id']=$id;
            hook("comment_after_save", $row);
            hook("comment_after_save_" . $type, $row);
        }
        return $id;
    }

    /*****删除一个评论*****/
    public function del($id = "")
    {
       
        $data = $this->where(array('id'=>$id))->find();
        hook("comment_before_del", $data);
        hook("comment_before_del_" . $data['type'], $data);
        if ($data) {
            $bool = BoxModel("SnsComment")->where("id='$id'")->delete();
            hook("comment_after_del" . $data['type'], $data);
            hook("comment_after_del_", $data);
            return $bool;
        }
    }

    /*********评论列表********/
    public function get($where, $limit = "50", $orderBy = "id")
    {
        $where = $where ? $where : "1";
        $count = $this->where($where)->count();
        $page = setPager($count, $limit);
        $limit = $page['firstRow'] . "," . $page['listRows'];
        $data = $this->where($where)->field("id")->limit($limit)->order($orderBy)->select();
        if ($data) {
            foreach ($data as $one) {
                $re[] = $this->load($one['id']);
            }
        }
        if ($data) {
            $data = array('count' => $count, 'list' => $re, 'pager' => $page);
        }
        return $data;
    }


}
 